package com.javasm.commons.config;

import com.javasm.commons.interceptors.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//WebMvcAutoConfiguration预配置类已经对WebMvcConfigurer接口中很多方法进行实现.
//拦截器配置
@Component
public class HandlerInterceptorConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")// 拦截路径
                .excludePathPatterns("/sysUser/login","/login","/sysUser/valicode","/sysUser/checkValicode");// 过滤拦截路径
    }

    //不建议使用,相当于springmvc.xml   <mvc:cors>
    //自定义拦截器中一定要OPTIONS预检请求放行
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//                .allowedOrigins("http://localhost:8031")
//                .allowedHeaders("*")
//                .allowedMethods("*")
//                .exposedHeaders("admin_token")
//                .maxAge(3600);
//    }
}
